﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class manager_systemSetting_roleManager : System.Web.UI.Page
{
    DBHelper db = new DBHelper();
    public string Html_Role = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }

    private void bindData()
    {
        string sql = "SELECT ID,roleName,isUsing FROM [Role] ORDER BY ID DESC";
        DataTable dt = db.getList(sql);
        rptmoduleManager.DataSource = dt;
        rptmoduleManager.DataBind();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (hidBID.Value.Trim() != "")
        {
            string roleID = StringHelper.SafeSqlInput(hidBID.Value);

            string deleteSql = "DELETE [Role] WHERE ID=" + roleID;
            int i = db.executeNoneQuery(deleteSql);
            if (i > 0)
            {
                deleteSql = "DELETE FROM [dbo].[RolePermissions] WHERE roleID=" + roleID;

                db.executeNoneQuery(deleteSql);

                CacheHelper.removeCache(AppConfig.CACHE_ROLE_PERMISSIONS + roleID);
                CacheHelper.removeCache(AppConfig.CACHE_MODULE_WITH_ROLE_PERMISSIONS + roleID);
                CacheHelper.removeCache(AppConfig.CACHE_NAVIGATION + roleID);

                Response.Write(CommonHelper.alertMsgAndRedirect("删除成功", Request.Url.LocalPath + Request.Url.Query));
            }
            else
            {
                Response.Write(CommonHelper.alertMsg("删除失败"));
            }
        }
    }

    protected void btnGet_Click(object sender, EventArgs e)
    {
        if (hidBID.Value.Trim() != "")
        {
            DataTable dt = db.getList("1", "[Role]", "ID,roleName,isUsing", "ID=" + StringHelper.SafeSqlInput(hidBID.Value), "ID ASC");
            if (dt.Rows.Count > 0)
            {
                txtName.Text = dt.Rows[0]["roleName"].ToString().Trim();
                ddlIsUsing.SelectedValue = dt.Rows[0]["isUsing"].ToString().Trim() == "True" ? "1" : "0";
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string insertSql = "INSERT INTO [Role] (roleName,isusing) VALUES (N'" + StringHelper.SafeSqlInput(txtName.Text.Trim()) + "'," + StringHelper.SafeSqlInput(ddlIsUsing.SelectedValue) + ")";

        int i = db.executeNoneQuery(insertSql);
        if (i > 0)
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("添加成功", Request.Url.LocalPath + Request.Url.Query));
        }
        else
        {
            Response.Write(CommonHelper.alertMsg("添加失败"));
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (hidBID.Value.Trim() != "")
        {
            string updateSql = "UPDATE [Role] SET roleName=N'" + StringHelper.SafeSqlInput(txtName.Text.Trim()) + "',isusing=" + StringHelper.SafeSqlInput(ddlIsUsing.SelectedValue) + " WHERE ID=" + StringHelper.SafeSqlInput(hidBID.Value);
            int i = db.executeNoneQuery(updateSql);
            if (i > 0)
            {
                Response.Write(CommonHelper.alertMsgAndRedirect("修改成功", Request.Url.LocalPath + Request.Url.Query));
            }
            else
            {
                Response.Write(CommonHelper.alertMsg("修改失败"));
            }
        }
    }

    protected void btnGetPermissions_Click(object sender, EventArgs e)
    {
        string txt_roleid = hidRoleID.Value.Trim();
        string sql = "select c.* from";
        sql += " (";
        sql += " select a.*, 'I' as sign from";
        sql += " (";
        sql += " select id, moduleName, id as pxid, 'A' as usign,[index] from[dbo].[Module] where parentID = 0";
        sql += " union";
        sql += " select id, moduleName, parentID as pxid, 'B' as usign,[index] from[dbo].[Module] where parentID > 0";
        sql += " ) a where a.id not in ";
        sql += " (";
        sql += " select moduleID from[dbo].[RolePermissions] where roleID = " + txt_roleid;
        sql += " )";
        sql += " union";
        sql += " select b.*,'D' as sign from";
        sql += " (";
        sql += " select id, moduleName, id as pxid, 'A' as usign,[index] from[dbo].[Module] where parentID = 0";
        sql += " union";
        sql += " select id, moduleName, parentID as pxid, 'B' as usign,[index] from[dbo].[Module] where parentID > 0";
        sql += " ) b where b.id in ";
        sql += " (";
        sql += " select moduleID from[dbo].[RolePermissions]";
        sql += " where roleID = " + txt_roleid;
        sql += " )) c order by c.pxid, c.usign, c.[index]";

        DataTable dt = db.getList(sql);

        Html_Role += "<table class=\"table table-bordered table-striped\">";
        Html_Role += "<tbody>";
        int i = 0;
        int j = 1;
        string bhstr = "";
        string sestr = "";
        foreach (DataRow dr in dt.Rows)
        {
            sestr = "";
            if (dr["sign"].ToString() == "D")
            {
                Trolestr.Value = Trolestr.Value + "|" + dr["ID"].ToString();
                sestr = "checked=\"checked\"";
            }
            if (dr["usign"].ToString() == "A")
            {
                if (i > 0)
                {
                    Html_Role += "</td></tr></tbody></table></td></tr>";
                }
                Html_Role += "<tr><td style=\"font-size:16px;font-weight:bold\">";
                Html_Role += "<input onclick=\"ClSe('P','" + dr["sign"].ToString() + "','" + dr["id"].ToString() + "',this,'" + dr["id"].ToString() + "')\"" + sestr + " id=\"parentt_" + dr["id"].ToString() + "\" type=\"checkbox\" value=\"P_" + dr["sign"].ToString() + "_" + dr["id"].ToString() + "\">";
                Html_Role += dr["moduleName"].ToString();
                Html_Role += "</td></tr>";
                Html_Role += "<tr><td style=\"padding-left: 20px;font-size:14px;\"><table class=\"table no-border no-margin\"><tbody><tr><td>";
                i++;
                j = 1;
            }
            else if (dr["usign"].ToString() == "B")
            {
                bhstr = "";
                if (j % 5 == 0)
                {
                    bhstr = "</br></br>";
                }

                Html_Role += bhstr + "<input  onclick=\"ClSe('C','" + dr["sign"].ToString() + "','" + dr["id"].ToString() + "',this,'" + dr["pxid"].ToString() + "')\"" + sestr + " style=\"margin-left:25px;\" name=\"childd_" + dr["pxid"].ToString() + "\" type=\"checkbox\" value=\"C_" + dr["sign"].ToString() + "_" + dr["id"].ToString() + "\">";
                Html_Role += dr["moduleName"].ToString();
                j++;
            }

        }
        Html_Role += "</td></tr></tbody></table></td></tr>";
        Html_Role += "</tbody>";
        Html_Role += "</table>";

    }

    protected void btnRoleSave_Click(object sender, EventArgs e)
    {

        string roleID = hidRoleID.Value;
        if (TrolestrID_I.Value.Trim() != "")
        {
            string[] addModuleIDs = TrolestrID_I.Value.TrimStart('|').TrimEnd('|').Split('|');
            foreach (string addId in addModuleIDs)
            {
                string sql = "INSERT INTO [dbo].[RolePermissions]([roleID],[moduleID]) VALUES (" + roleID + "," + addId + ")";
                db.executeNoneQuery(sql);
            }

            CacheHelper.removeCache(AppConfig.CACHE_ROLE_PERMISSIONS + roleID);
            CacheHelper.removeCache(AppConfig.CACHE_MODULE_WITH_ROLE_PERMISSIONS + roleID);
            CacheHelper.removeCache(AppConfig.CACHE_NAVIGATION + roleID);
        }

        if (TrolestrID_D.Value.Trim() != "")
        {
            string delModuleIDs = TrolestrID_D.Value.TrimStart('|').TrimEnd('|').Replace('|', ',');

            string sql = "DELETE FROM [dbo].[RolePermissions] WHERE moduleID IN (" + delModuleIDs + ") AND roleID=" + roleID;
            db.executeNoneQuery(sql);

            CacheHelper.removeCache(AppConfig.CACHE_ROLE_PERMISSIONS + roleID);
            CacheHelper.removeCache(AppConfig.CACHE_MODULE_WITH_ROLE_PERMISSIONS + roleID);
            CacheHelper.removeCache(AppConfig.CACHE_NAVIGATION + roleID);
        }

        Response.Write(CommonHelper.alertMsgAndRedirect("修改成功", Request.Url.LocalPath + Request.Url.Query));
    }
}